#!/usr/bin/env python
#coding=utf-8

import SocketServer
import time
class FTPServiceHandler(SocketServer.BaseRequestHandler):
	def handle(self):
		while 1:
			get_data = self.request.recv(1024).strip()
			if not get_data: break
			cmd, filename = get_data.split()[:2]
			print 'file is %s' % get_data
			if cmd != 'get':
				raise
			ifh = open(filename, 'rb')
			ifh.seek(0,2)
			len_data = ifh.tell()
			ifh.seek(0)
			data = ifh.read(1024)
			first = 1
			while data:
				if first:
					self.request.sendall('%s\t%s' % (len_data, data))
					first = 0
				else:
					self.request.sendall(data)
				data = ifh.read(1024)

if __name__ == '__main__':
	host, port = '', 50010
	server = SocketServer.ForkingTCPServer((host, port), FTPServiceHandler)
	server.allow_reuse_address=True
	server.serve_forever()